package gui;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;

import Classes.Clients;
import Classes.ListenerChat;
import Classes.Usuarios;
import javax.swing.border.EtchedBorder;

/**
 * An example which shows off a functional simple text editor.  Includes a variety of events.
 */
public class ChatWindow extends JFrame implements Runnable {

	private javax.swing.JPanel jContentPane = null;
	private javax.swing.JScrollPane jScrollPane = null;
	private javax.swing.JTextArea jTextArea = null;
	private javax.swing.JScrollPane jSPMessage = null;
	private javax.swing.JTextArea jTAMessage = null;
	private static final String title = "Chat Window with ";
	private String chatWith = "";  //  @jve:decl-index=0:
	private Usuarios Origem = null;
	private Usuarios Destino = null;
	private JButton jBSend = null;  //  @jve:decl-index=0:visual-constraint="585,93"
	/**
	 * This method initializes 
	 * 
	 */
	public ChatWindow() {
		super();
		initialize();
	}
	
	public ChatWindow(Usuarios Orig, Usuarios Dest) {
		super();
		this.chatWith = new String(Dest.getNome());
		this.Origem = Orig;
		this.Destino = Dest;
		initialize();
	}

	/**
	 * This method initializes jBSend	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJBSend() {
		if (jBSend == null) {
			jBSend = new JButton();
			jBSend.setSize(new Dimension(83, 55));
			jBSend.setText("Send");
			jBSend.setMnemonic(KeyEvent.VK_S);
			jBSend.setLocation(466, 223);
			jBSend.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					sendMessage();
				}
			});
		}
		return jBSend;
	}

	public static void main(String[] args) {
		ChatWindow ste = new ChatWindow();
		ste.setVisible(true);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private javax.swing.JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new javax.swing.JPanel();
			jContentPane.setLayout(null);
			jContentPane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
			jContentPane.setBorder(new SoftBevelBorder(SoftBevelBorder.RAISED));
			jContentPane.add(getJScrollPane(), null);
			jContentPane.add(getJBSend(), null);
			jContentPane.add(getJSPMessage(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setContentPane(getJContentPane());
		this.setSize(560, 315);
		this.setResizable(false);
		this.setTitle(this.title + this.chatWith);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
//		this.addWindowListener(new java.awt.event.WindowAdapter() {
//			public void windowClosing(java.awt.event.WindowEvent e) {
//				doExit();
//			}
//		});
		ListenerChat lc = new ListenerChat(Destino.getUDPport(), Destino.getNome(), getJTextArea());
		Thread generalListener = new Thread(lc);
		generalListener.start();
	}

	/**
	 * This method initializes jScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private javax.swing.JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new javax.swing.JScrollPane();
			jScrollPane.setBounds(new Rectangle(5, 5, 545, 215));
			jScrollPane.setViewportView(getJTextArea());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jTextArea
	 * 
	 * @return javax.swing.JTextArea
	 */
	private javax.swing.JTextArea getJTextArea() {
		if (jTextArea == null) {
			jTextArea = new javax.swing.JTextArea();
			jTextArea.setEditable(false);
			jTextArea.setTabSize(4);
			jTextArea.setFont(new Font("SansSerif", Font.BOLD, 14));
			jTextArea.setForeground(Color.blue);
			jTextArea.setLineWrap(true);
		}
		return jTextArea;
	}


	/**
	 * This method initializes jScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private javax.swing.JScrollPane getJSPMessage() {
		if (jSPMessage == null) {
			jSPMessage = new javax.swing.JScrollPane();
			jSPMessage.setBounds(new Rectangle(5, 223, 455, 56));
			jSPMessage.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
			jSPMessage.setViewportView(getJTAMessage());
		}
		return jSPMessage;
	}

	/**
	 * This method initializes jTextArea
	 * 
	 * @return javax.swing.JTextArea
	 */
	private javax.swing.JTextArea getJTAMessage() {
		if (jTAMessage == null) {
			jTAMessage = new javax.swing.JTextArea();
			jTAMessage.setTabSize(4);
			jTAMessage.setFont(new Font("Arial", Font.BOLD, 14));
			jTAMessage.setWrapStyleWord(true);
			jTAMessage.setForeground(new Color(255, 139, 0));
			jTAMessage.setFocusable(true);
			jTAMessage.setLineWrap(true);
			jTAMessage.addKeyListener(new java.awt.event.KeyAdapter() {
				public void keyTyped(java.awt.event.KeyEvent e) {
					if ( jTAMessage.getText().length() >= 200 ){
						getJTAMessage().setText(getJTAMessage().getText().substring(0, 199));
					}
				}
			});
		}
		return jTAMessage;
	}
	
	/*
	 * TODO sendMessage()
	 */
	private void sendMessage() {
			String msg = this.Origem.getNome()+ " diz : " + getJTAMessage().getText();
			getJTextArea().append(msg+"\n");
			getJTAMessage().setText("");
			System.out.println("DESTINO GET IP = " + Destino.getIP());
			Clients.sendMessageTo(msg, Destino.getIP(), Origem.getUDPport());
	}

	@Override
	public void run() {
		this.setVisible(true);
		
		System.out.println("DESTINO NOME = " + this.Destino.getNome() +
			" PORTA = " + this.Destino.getUDPport());
		System.out.println("ORIGEM NOME = " + this.Origem.getNome() +
				" PORTA = " + this.Origem.getUDPport());
	}
	
//	public void doExit(){
//		this.setVisible(false);
//	}
	
} //  @jve:visual-info  decl-index=0 visual-constraint="20,27"
